﻿using System;
using System.Collections.Generic;
using System.Collections;
using CommonTypes.Client;
using CommonTypes.Server;
using Client.Model;
using System.Runtime.Remoting;

namespace Client.RemoteObjects
{
    public class PMToClient : MarshalByRefObject, IPMToClient
    {
        private ClientData _clientData;

        private ClientToClient _clientToClientProxy;

        private ServerToClient _serverToClientProxy;

        public PMToClient(ClientData cd, ClientToClient clientToClient, ServerToClient serverToClient)
        {
            _clientData = cd;
            _clientToClientProxy = clientToClient;
            _serverToClientProxy = serverToClient;
        }
        
        public bool Connect()
        {
            if (_clientData.IsConnected == false)
            {
                RemotingServices.Marshal(_clientToClientProxy, "ClientToClient");
                RemotingServices.Marshal(_serverToClientProxy, "ServerToClient");
                _clientData.IsConnected = true;
                _clientData.ResumeBreakReservations();
                Console.WriteLine("Client Successfully connected.");
                return true;
            }
            else
                return false;
        }

        public bool Disconnect()
        {
            if (_clientData.IsConnected == true)
            {
                RemotingServices.Disconnect(_clientToClientProxy);
                RemotingServices.Disconnect(_serverToClientProxy);
                _clientData.StopInitiatedReservations();
                _clientData.IsConnected = false;
                Console.WriteLine("Client Successfully disconnected.");
                return true;
            }
            else
                return false;            
        }

        public void BeginReservation(ArrayList slotNumbers, String clientsToInvite, String reservationName)
        {
            if (clientsToInvite.Length == 0)
            {
                return;
            }

            ArrayList slotList = new ArrayList();

            for (int index = 0; index < slotNumbers.Count; index++)
            {
                ReservationSlot slot = new ReservationSlot((int)slotNumbers[index]);
                slotList.Add(slot);
            }

            String[] invitedUsersString = clientsToInvite.Split(',');
            ArrayList invitedUsersList = new ArrayList();

            for (int index = 0; index < invitedUsersString.Length; index++)
            {
                invitedUsersList.Add(invitedUsersString[index]);
            }

            Reservation reservation = new Reservation(_clientData.ClientId, 0, slotList, invitedUsersList, reservationName);

            _clientData.AddNonInitiatedReservation(reservation);

            IClientToServer clientToServer = _clientData.GetRandomServer();
            clientToServer.GenerateTicket(_clientData.ClientId, reservation.ReservationTimestamp);
        }

        public ArrayList ReadCalendar()
        {
            return _clientData.ClientSlots;
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
    }
}
